package fc.sj.study.presenter;

import android.os.Handler;

import fc.sj.study.Bean.UserBean;
import fc.sj.study.Model.IUser;
import fc.sj.study.UserLoginView;
import fc.sj.study.biz.UserBiz;
import fc.sj.study.biz.UserLoginListener;

/**
 * Created by Fengchao on 2017/5/6.
 */

public class UserLoginPresenter {
    private UserBiz userBiz;
    private UserLoginView userLoginView;
    private Handler mHandler = new Handler();

    public UserLoginPresenter(UserLoginView userLoginView)
    {
        this.userLoginView = userLoginView;
        this.userBiz = new IUser();
    }

    public void login()
    {
        userLoginView.showLoading();
        userBiz.Login(userLoginView.getUserName(), userLoginView.getPassword(), new UserLoginListener()
        {
            @Override
            public void LoginSuccess(final UserBean user) {
                mHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userLoginView.toMainActivity(user);
                        userLoginView.hideLoading();
                    }
                });
            }

            @Override
            public void LoginFaild() {
                //需要在UI线程执行
                mHandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userLoginView.showFailedError();
                        userLoginView.hideLoading();
                    }
                });
            }
        });
    }

    public void clear()
    {
        userLoginView.clearUserName();
        userLoginView.clearPassword();
    }

}
